﻿/*
	flad : Copyright 2010 Dale J Williams

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package lads.flad.input {
	
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	
	/**
	 * KeyboardManager: A quick keyboard polling class.
	 * @author Dale J Williams
	 * @version 1.0
	 */
	public class KeyboardManager extends EventDispatcher {
		
		private var _stage	:Stage;
		private var _keys	:Object;
		
		public function KeyboardManager($stage:Stage) {
			_stage = $stage;
			init();
		}
		
		private function init():void {
			_keys = { };			
			_stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeys);
			_stage.addEventListener(KeyboardEvent.KEY_UP, handleKeys);
		}
		
		private function handleKeys(e:KeyboardEvent):void {
			if (e.type == KeyboardEvent.KEY_DOWN) 
				_keys[e.keyCode] = e.keyCode;
			else delete _keys[e.keyCode];
			dispatchEvent(e);
		}
		
		public function isDown($keyCode:uint):Boolean {
			return _keys[$keyCode] != null;
		}
		
		public function get keysDown():Object {
			return _keys;
		}
		
	}

}